package com.gitee.payment.controller;


import com.gitee.bodboy.entities.order.Payment;
import com.gitee.bodboy.result.CommonResult;
import com.gitee.payment.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 订单表(Payment)表控制层
 *
 * @author makejava
 * @since 2020-12-10 17:18:46
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
    /**
     * 服务对象
     */
    @Autowired
    private PaymentService paymentService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public CommonResult<Payment> selectOne(Integer id) {
        return CommonResult.ok(paymentService.queryById(id), 200 ,"获取数据成功!-----");
    }

    @PostMapping("/save")
    public CommonResult<Payment> saveOne(@RequestBody Payment payment){
        return CommonResult.ok(paymentService.insert(payment) ,200 , "数据插入成功!");
    }

    @GetMapping("/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        services.stream().forEach(e -> log.info("service-element :{}" ,e));
        List<ServiceInstance> payment = discoveryClient.getInstances("PAYMENT");
        payment.stream().forEach(e -> log.info(e.getServiceId() + "\t" + e.getHost() +
                "\t" + e.getPort() + "\t" + e.getUri()));
        return discoveryClient;

    }

    @GetMapping(value = "/feign/timeout")
    public String paymentFeignTimeout() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return serverPort;
        }
    }


    @GetMapping("/zipkin")
    public String zipkin(){
        return "this is zipkin test menthod";
    }
}